extends Node2D

@onready var tileMap=$TileMap
@onready var camera_2d:Camera2D=$玩家/Camera2D
# 装饰的图集坐标
const star=Vector2i(18,1)
const flower=Vector2i(9,3)
const leaf=Vector2i(8,4)
const stone=Vector2i(6,2)
const bigStone=Vector2i(5,2)
const mushroom=Vector2i(9,4)

func _ready() -> void:
	getRect()
	randomTile()
	pass

# 生成随机装饰背景
func randomTile():
	var cells=tileMap.get_used_cells(0)
	tileMap.clear_layer(1)
	var ran=RandomNumberGenerator.new()
	for cellPos in cells:
		var num=ran.randi_range(0,100)
		if num<=0:
			# 图层 绘制坐标 图集id 图集坐标
			tileMap.set_cell(1,cellPos,0,flower) 
		elif num<=2:
			tileMap.set_cell(1,cellPos,0,leaf) 
		elif num<=3:
			tileMap.set_cell(1,cellPos,0,stone) 
		elif num<=4:
			tileMap.set_cell(1,cellPos,0,bigStone) 
		elif num<=5:
			tileMap.set_cell(1,cellPos,0,mushroom) 
		elif num<=10:
			tileMap.set_cell(1,cellPos,0,star) 

# 获取地图的边界
func getRect():
	# 获得图块为单位的矩形宽高
	var used=tileMap.get_used_rect()
	# 单个图块的像素大小
	var tile_size=tileMap.tile_set.tile_size
	camera_2d.limit_top = used.position.y*tile_size.y*tileMap.scale.y
	camera_2d.limit_left = used.position.x*tile_size.x*tileMap.scale.x
	camera_2d.limit_right = used.end.x*tile_size.x*tileMap.scale.x
	camera_2d.limit_bottom = used.end.y*tile_size.y*tileMap.scale.y

	camera_2d.reset_smoothing()
	pass
